<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<!--多选下拉列表框-->
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

	<!--        Toastr提示信息5-->
	<script src="/js/toastr/toastr.min.js"></script>
	<link href="/js/toastr/toastr.css" rel="stylesheet">
	<!--bootstrap-dialog模态框，一般用于增加修改弹出一个窗口-->
	<script src="/js/bootstrap-dialog/dist/js/bootstrap-dialog.js"></script>
	<link href="/js/bootstrap-dialog/dist/css/bootstrap-dialog.css" rel="stylesheet">

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" onclick="addspec()"  data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteid()" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="typeTemplate">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->
  <script>

	  $(function (){

	  	getdata();

		  //多选下拉列表
		  $("#brands").select2();
		  $("#specs").select2();
		  $("#upspecs").select2();
		  $("#upbrands").select2();
	  })


	  //查询全部
	  function getdata(){
		  $.ajax({
			  url:"/type/getall",
			  type:"get",
			  dataType:"json",
			  data:{},
			  async:false,
			  success:function (result){
				  // console.log(result)
				  if(result.code==10000){
					  var html="";
					  var type=result.data;
					  // console.log(type)
					  for(var i=0;i<type.length;i++){
						  html+='<tr>';
						  html+=' <td><input id="checkid" type="checkbox"  value="'+type[i].id+'"  ></td>';
						  html+='<td>'+type[i].id+'</td>';
						  html+='<td>'+type[i].name+'</td>';
						  html+='<td>';
						  var brandlist=JSON.parse(type[i].brandIds);//将字符串转换为数组,获取到brand数组
						  for(var j=0;j<brandlist.length;j++){
							  if(j==brandlist.length-1){
								  html+=brandlist[j].text
							  }else{
								  html+=brandlist[j].text+','
							  }
						  }
						  html+='</td>';
						  html+='<td>';
						  var specIds=JSON.parse(type[i].specIds);//将字符串转换为数组,获取到specIds数组
						  for(var p=0;p<specIds.length;p++){
							  if(p==specIds.length-1){
								  html+=specIds[p].text
							  }else{
								  html+=specIds[p].text+','
							  }
						  }
						  html+='</td>';
						  html+='<td>';
						  var custom=JSON.parse(type[i].customAttributeItems);//将字符串转换为数组,customAttributeItems数组
						  for(var j=0;j<custom.length;j++){
							  if(j==custom.length-1){
								  html+=custom[j].text
							  }else{
								  html+=custom[j].text+','
							  }
						  }
						  html+='</td>';
						  html+='<td class="text-center">';
						  html+='<button type="button" class="btn bg-olive btn-xs" onclick="updatespec('+type[i].id+')" data-toggle="modal" data-target="#updatespectb" >修改</button>' +
								  '<button type="button" class="btn bg-olive btn-xs" onclick="deleteid('+type[i].id+')" data-toggle="modal" >删除</button>';
						  html+='</td>';
						  html+='</tr>';
					  }
					  $("#typeTemplate").html(html)
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }






	  //增加
	  function addtype(){
	  	//增加关联品牌
	  	var brandlist=$("#brands").select2('data');//获取到选中的所有数据
		  var brandarr=[];
		  $(brandlist).each(function (i,v){//遍历取出里面数据存入json
		  	var json={id:v.id,text:v.text};
		  	brandarr.push(json)//将json加入数组中
		  })
		  var brandJson=JSON.stringify(brandarr)//转换为string类型
		  //增加规格
		  var speclist=$("#specs").select2('data');
		  var specarr=[]
		  $(speclist).each(function (i,v){
		  	var jsons={id:v.id,text:v.text};
		  	specarr.push(jsons)
		  })
		  var specJson=JSON.stringify(specarr)//转换为string类型
		  //获取扩展属性
		  var trlist=$("#tobody").find("tr")//意思寻找这个cptbody下面的tr
		  var arr=[]//设置一个数组存每一个对象
		  $(trlist).each(function (i,v){
			  var tdlist=$(v).find("td");//获取到td数组
			  var optionName=$(tdlist[0]).find("input").val();
			  var json={text:optionName};//json格式
			  arr.push(json)
		  })
		  var specOptionJson=JSON.stringify(arr)//将json格式转换为string格式，传输后台,获取扩展属性
		  //获取商品类型
		  var name=$("#name").val()
		  $.ajax({
			  url:"/type/addspec",
			  type:"post",
			  dataType:"json",
			  data:{name:name,specIds:specJson,brandIds:brandJson,customAttributeItems:specOptionJson},
			  async:false,
			  success:function (result){
				  console.log(result)
				  if(result.code==10000){
					  toastr.success("更新成功")
					  dialog.close();//更新成功后关闭
					  getdata();
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }

	  //修改回显
	  function updatespec(id){
		  $.ajax({
			  url:"/type/updatespec",
			  type:"post",
			  dataType:"json",
			  data:{id:id},
			  async:false,
			  success:function (result){
				  if(result.code==10000){
					var datas=result.data
					  // console.log(datas)
					 $("#upname").val(datas.name);
					$("#ids").val(datas.id);
					//品牌
					var brandlist= JSON.parse( datas.brandIds);//将字符串转换为对象
					  var brandarr=[]
					  $(brandlist).each(function (i,v){
					  	brandarr.push(v.id);
					  })
					  //下拉列表的回显方式
					  $("#upbrands").val(brandarr).trigger("change")//品牌回显
					  //规格
					 var specIdslist= JSON.parse(datas.specIds);
					  var specarr=[]
					  $(specIdslist).each(function (i,v){
					  	specarr.push(v.id)
					  })
					  $("#upspecs").val(specarr).trigger("change")//规格回显
					  //扩展属性
					  var custom=JSON.parse(datas.customAttributeItems);
					  var s=""
					  for(var i=0;i<custom.length;i++){
					  	s+=' <tr id="upcptr">\n' +
								'\t\t\t\t\t\t\t\t\t  <td> <input type="text" class="form-control" value="'+custom[i].text+'" name="text" placeholder="规格选项"></td>\n' +
								'\t\t\t\t\t\t\t\t\t  <td> <button type="button" class="btn btn-default" onclick="deleterow(this)" ><i class="fa fa-file-o"></i>删除</button></td>\n' +
								'\t\t\t\t\t\t\t\t  </tr>'
					  }
					  $("#uptobody").html(s)//回显扩展属性
				  }else{
				  	alert(result.message)
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })


	  }

	  //修改保存
	  function updatetupe(){
		  //增加关联品牌
		  var brandlist=$("#upbrands").select2('data');//获取到选中的所有数据
		  var brandarr=[];
		  $(brandlist).each(function (i,v){//遍历取出里面数据存入json
			  var json={id:v.id,text:v.text};
			  brandarr.push(json)//将json加入数组中
		  })
		  var brandIds=JSON.stringify(brandarr)//转换为string类型
		  //增加规格
		  var speclist=$("#upspecs").select2('data');
		  var specarr=[]
		  $(speclist).each(function (i,v){
			  var jsons={id:v.id,text:v.text};
			  specarr.push(jsons)
		  })
		  var specIds=JSON.stringify(specarr)//转换为string类型
		  //修改扩展属性
		  var trlist=$("#uptobody").find("tr")//意思寻找这个cptbody下面的tr
		  var arr=[]//设置一个数组存每一个对象
		  //遍历tr，获取tr下面每个td里面的input
		  $(trlist).each(function (i,v){
			  var tdlist=$(v).find("td");//获取到td数组
			  var text=$(tdlist[0]).find("input").val();
			  var json={text:text};//json格式
			  arr.push(json)
		  })
		  var customAttributeItems=JSON.stringify(arr)//将json格式转换为string格式，传输后台
		  console.log(customAttributeItems)
		  var name=$("#upname").val()//商品类型
		  var id=$("#ids").val()//id
		  $.ajax({
			  url:"/type/uptype",
			  type:"post",
			  dataType:"json",
			  data:{name:name,specIds:specIds,brandIds:brandIds,customAttributeItems:customAttributeItems,id:id},
			  async:false,
			  success:function (result){
				  console.log(result)
				  if(result.code==10000){
					  toastr.success("更新成功")
					  dialog.close();//更新成功后关闭

				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })


	  }


	  //删除
	  function deleteid(id){
		  // alert(id)
		  $.ajax({
			  url:"/type/deleteid",
			  type:"post",
			  dataType:"json",
			  data:{id:id},
			  async:false,
			  success:function (result){
				  console.log(result)
				  if(result.code==10000){
					  toastr.success("删除成功")
					  dialog.close();//删除成功后关闭
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })


	  }


	  //增加一行
	  function addbook(){
		  var cloneRow=$("#cptr").clone();
		  $("#tobody").append(cloneRow)
		  $(cloneRow).find(":input").val("");
	  }

	  //删除一行
	  function deleterow(cc){
		  var table = cc.parentNode.parentNode.parentNode;
		  var td=cc.parentNode.parentNode;
		  table.removeChild(td);
	  }


  </script>



  <!-- 修改窗口 -->
  <div class="modal fade" id="updatespectb" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updatespectbd">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">
				  <input type="hidden" id="ids" name="ids">
				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td><input   id="upname" name="name" class="form-control" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select name="brandIds" id="upbrands" style=" width: 100%" multiple>
								  <option  th:each="brand :${b}"  th:value="${brand.id}" th:text="${brand.name}" ></option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="upspecs" name="specIds" style=" width: 100%" multiple>
								  <option  th:each="spec :${s}"  th:value="${spec.id}" th:text="${spec.specName}" ></option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" onclick="addbook()" >新增规格 </button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="uptobody">
								  <tr id="upcptr">
									  <td> <input type="text" class="form-control"  name="text" placeholder="规格选项"></td>
									  <td> <button type="button" class="btn btn-default" onclick="deleterow(this)" ><i class="fa fa-file-o"></i>删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>
				  </table>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updatetupe()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
					    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  id="name" name="name" class="form-control" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
						<select name="brandIds" id="brands" style=" width: 100%" multiple>
							<option  th:each="brand :${b}"  th:value="${brand.id}" th:text="${brand.name}" ></option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specs" name="specIds" style=" width: 100%" multiple>
							<option  th:each="spec :${s}"  th:value="${spec.id}" th:text="${spec.specName}" ></option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
							<button type="button" class="btn btn-default" onclick="addbook()" >新增规格 </button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="tobody">
							<tr id="cptr">
								<td> <input type="text" class="form-control"  name="text" placeholder="规格选项"></td>
								<td> <button type="button" class="btn btn-default" onclick="deleterow(this)" ><i class="fa fa-file-o"></i>删除</button></td>
							</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>
			 </table>
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="addtype()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>


    
</body>

</html>